﻿// My methods here.
//-----------------------------------------------------------------------
// <Copyright file=LienHe company=DHPD>
// Copyright (c) DHPD Corporation (http://dhpd.edu.vn). All rights reserved.
// </copyright>
// <author>HP</author>
//-----------------------------------------------------------------------


using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using KM.DAT;
using KM.ENT;
using KM.Models;

namespace KM.DAT
{
    /// <summary>
    /// This is datLienHe class
    /// </summary>

    public class datLienHe
    {
        public datLienHe()
        {
            //
            // TODO: Add constructor logic here
            //
        }
        SqlCommand cmd;
        SqlDataReader dr;
        string constr = System.Configuration.ConfigurationManager.ConnectionStrings["conStr"].ConnectionString;
        #region  === tạo mới và sửa LienHe===
        public void createEdit(entLienHe entLH)
        {
            SqlConnection sqlConnection = new SqlConnection(constr);
            sqlConnection.Open();
            cmd = new SqlCommand("sp_LienHeCreateEdit", sqlConnection);
            cmd.CommandType = CommandType.StoredProcedure;
            SqlParameter[] sqla = new SqlParameter[5];
            sqla[0] = new SqlParameter("Id", DbType.Int32);
            sqla[1] = new SqlParameter("HoTen", DbType.Int32);
            sqla[2] = new SqlParameter("DienThoai", DbType.Int32);
            sqla[3] = new SqlParameter("Email", DbType.Int32);
            sqla[4] = new SqlParameter("NoiDung", DbType.Int32);

            sqla[0].Value = entLH.Id;
            sqla[1].Value = entLH.HoTen;
            sqla[2].Value = entLH.DienThoai;
            sqla[3].Value = entLH.Email;
            sqla[4].Value = entLH.NoiDung;
            


            cmd.Parameters.AddRange(sqla);
            cmd.ExecuteNonQuery();
            sqlConnection.Close();
        }
        #endregion

        #region  === getall LienHe===
        public List<entLienHe> getAll()
        {
            SqlConnection sqlConnection = new SqlConnection(constr);
            sqlConnection.Open();
            cmd = new SqlCommand("sp_LienHeGetAll", sqlConnection);
            cmd.CommandType = CommandType.StoredProcedure;

            dr = cmd.ExecuteReader();
            entLienHe entLH = new entLienHe();
            List<entLienHe> lst = new List<entLienHe>();
            while (dr.Read())
            {
                entLH = new entLienHe();
                entLH.Id = Convert.ToInt32(dr["Id"].ToString());
                entLH.HoTen = dr["HoTen"].ToString();
                entLH.DienThoai = dr["DienThoai"].ToString();
                entLH.Email = dr["Email"].ToString();
                entLH.NoiDung = dr["NoiDung"].ToString();
                entLH.NgayTao = dr["NgayTao"].ToString();
                entLH.TrangThai = Convert.ToByte(dr["TrangThai"].ToString());

                lst.Add(entLH);
            }
            dr.Close();
            sqlConnection.Close();
            return lst;
        }
        #endregion

        #region  === getinfo LienHe===
        public entLienHe getInfo(int Id)
        {
            SqlConnection sqlConnection = new SqlConnection(constr);
            sqlConnection.Open();
            cmd = new SqlCommand("sp_LienHeGetInfo", sqlConnection);
            cmd.CommandType = CommandType.StoredProcedure;
            SqlParameter[] sqla = new SqlParameter[1];
            sqla[0] = new SqlParameter("Id", DbType.Int32);
            sqla[0].Value = Id;

            cmd.Parameters.AddRange(sqla);
            dr = cmd.ExecuteReader();
            entLienHe entLH = new entLienHe();
            if (dr.Read())
            {
                entLH.Id = Convert.ToInt32(dr["Id"].ToString());
                entLH.HoTen = dr["HoTen"].ToString();
                entLH.DienThoai = dr["DienThoai"].ToString();
                entLH.Email = dr["Email"].ToString();
                entLH.NoiDung = dr["NoiDung"].ToString();
                entLH.NgayTao = dr["NgayTao"].ToString();
                entLH.TrangThai = Convert.ToByte(dr["TrangThai"].ToString());

            }
            dr.Close();
            sqlConnection.Close();
            return entLH;
        }
        #endregion

        #region === xoa LienHe===
        public void delete(int id)
        {
            SqlConnection sqlConnection = new SqlConnection(constr);
            sqlConnection.Open();
            cmd = new SqlCommand("sp_LienHeDeleteById", sqlConnection);
            cmd.CommandType = CommandType.StoredProcedure;
            SqlParameter[] sqla = new SqlParameter[1];
            sqla[0] = new SqlParameter("Id", DbType.Int32);
            sqla[0].Value = id;

            cmd.Parameters.AddRange(sqla);
            cmd.ExecuteNonQuery();
            sqlConnection.Close();
        }
        #endregion
    }
}
